﻿<%@ WebHandler Language="C#" Class="User" %>

using System;
using System.Web;
using System.Web.SessionState;

public class User : IHttpHandler,IRequiresSessionState
{

    Spring.Context.IApplicationContext appContext = Spring.Context.Support.ContextRegistry.GetContext();
    GONES.Tools.Common.LoginInfo loginInfo = new GONES.Tools.Common.LoginInfo();
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string title = context.Request["title"];

        if (context.Session["LoginInfo"] != null)
        {
            loginInfo = (GONES.Tools.Common.LoginInfo)context.Session["LoginInfo"];
        }
        if (string.IsNullOrEmpty(title))
        {
            failure("无指定操作");
        }
        else
        {
            switch (title)
            {
                case "add":
                    if (Add(context.Request["username"], context.Request["userpassword"], context.Request["isdisabled"], context.Request["remark"]))
                    {
                        context.Response.Write(success("", "UsersList"));
                    }
                    else
                    {
                        context.Response.Write(failure("数据库连接错误"));
                    }
                    break;
                case "delete":
                    if (Del(context.Request["ids"]))
                    {
                        context.Response.Write(success("", "UsersList"));
                    }
                    else
                    {
                        context.Response.Write(failure("删除用户失败!"));
                    }
                    break;
                case "updata":
                    if (Updata(context.Request["userid"], context.Request["username"], context.Request["userpassword"], context.Request["isdisabled"], context.Request["remark"]))
                    {
                        context.Response.Write(success("", "UsersList"));
                    }
                    else
                    {
                        context.Response.Write(failure("修改失败!"));
                    }
                    break;
            }
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }


    public bool Del(string ids)
    {
        var userInfoManager = appContext.GetObject("S_UsersManager") as GONES.IBLL.IS_UsersManager;
        string[] strs = ids.Split(',');
        try
        {
            foreach (string str in strs)
            {
                userInfoManager.Delete(str);
            }
            return true;
        }
        catch { return false; }
    }

    private GONES.Model.S_Users GetUserModel(string id,string username, string userpassword, string isdisabled, string remark)
    {
         var userInfoManager = appContext.GetObject("S_UsersManager") as GONES.IBLL.IS_UsersManager;
        GONES.Model.S_Users users = new GONES.Model.S_Users();
        isdisabled = isdisabled == "on" ? "0" : "1";
        if (id != "")
           users= userInfoManager.Get(id);
        users.ID = id;
        users.UserName = username;
        users.UserPassWord = userpassword.Length == 32 ? 
        userpassword : GONES.Tools.Common.Rnd.MD5(GONES.Tools.Common.Rnd.MD5(userpassword));
        users.ReMark = remark;
        users.IsDisabled = int.Parse(isdisabled);
        return users;
    }
    public bool Add(string username, string userpassword, string isdisabled,string remark)
    {
        var userInfoManager = appContext.GetObject("S_UsersManager") as GONES.IBLL.IS_UsersManager;
        GONES.Model.S_Users users = new GONES.Model.S_Users();
        try
        {

            string id = "";
            users = GetUserModel(id, username, userpassword, isdisabled, remark);
            users.ID = GONES.Tools.Common.Rnd.Guid.ToString();
            users.CreateUser = loginInfo.UserName;
            userInfoManager.Add(users);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public bool Updata(string id, string username, string userpassword, string isdisabled, string remark)
    {
        var userInfoManager = appContext.GetObject("S_UsersManager") as GONES.IBLL.IS_UsersManager;
        GONES.Model.S_Users users = new GONES.Model.S_Users();
        try
        {
            users = GetUserModel(id, username, userpassword, isdisabled, remark);
            users.ModifyUser = loginInfo.UserName;
            userInfoManager.Edit(users);
            return true;
        }
        catch
        {
            return false;
        }
    }

    public string success()
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!\"}";
    }
    public string success(string str, string navTabId)
    {
        return "{\"statusCode\":\"200\", \"message\":\"操作成功!" + str + "\",\"navTabId\":\"" + navTabId + "\"}";
    }
    public string failure()
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败\"}";
    }
    public string failure(string str)
    {
        return "{\"statusCode\":\"300\", \"message\":\"操作失败!" + str + "\"}";
    }

}